//
//  WeatherDataRequest.m
//  RL_项目二
//
//  Created by rimi on 16/3/23.
//  Copyright © 2016年 ruifengpeixun. All rights reserved.
//

#import "WeatherDataRequest.h"
#import "YXHTTPRequst.h"
@interface WeatherDataRequest ()
@property(nonatomic, strong) NSMutableArray *juHeDataSource;
//当前实况天气，湿度，及详情接口 temperature温度humidity湿度 info详情
@property (nonatomic, strong) NSDictionary *weather;
//当天风向和级别接口 direct风向 power级别
@property (nonatomic, strong) NSDictionary *wind;
//生活指数接口  chuanyi 穿衣指数 ganmao 搞毛指数 kongtiao 空调指数 wuran 污染指数 xiche洗车指数 yundong 运动指数 ziwaixian 紫外线指数
@property (nonatomic, strong) NSDictionary *LifeIndex;
//pm2.5接口 curPm 静态颗粒物指数 pm25 漂浮颗粒物指数 pm10指数 level 级别 quality品质 des 评价
@property (nonatomic, strong) NSDictionary *pm;


@end

@implementation WeatherDataRequest

- (void)getData:(void(^)(void))block withcity:(NSString *)city {
    //转码
    NSString * encodingString = [city stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    //数据请求
    NSString *shuju = [NSString stringWithFormat:@"http://op.juhe.cn/onebox/weather/query?cityname=%@&key=7062213acbbae4a321ff056491b9f68d",encodingString];
    [[YXHTTPRequst shareInstance]networking:shuju parameters:nil method:YXRequstMethodTypeGET success:^(NSDictionary *obj) {
        NSLog(@"%@", obj[@"error_code"]);
        if ([obj[@"error_code"] intValue] == 0) {
    
            self.juHeDataSource = obj[@"result"][@"data"][@"weather"];
            self.weather = obj[@"result"][@"data"][@"realtime"][@"weather"];
            self.wind = obj[@"result"][@"data"][@"realtime"][@"wind"];
            self.LifeIndex = obj[@"result"][@"data"][@"life"][@"info"];
            self.pm = obj[@"result"][@"data"][@"pm25"][@"pm25"];
            
        }else{
            self.error = @"1";
        }
        if (block) {
            block();
        }
    } failsure:^(NSError *error) {
        if (![error isKindOfClass:[NSNull class]] ){
            if ([error  isEqual: @"207301"]) {
                //错误的城市名
                self.error = @"2";
            }
            if ([error  isEqual: @"207302"]) {
                //查询不到该城市信息
                self.error = @"3";
            }if ([error  isEqual: @"207303"]) {
                //网络错误
                self.error = @"4";
            }if ([error  isEqual: @"10001"]) {
                self.error = @"1";
            }if ([error  isEqual: @"10002"]) {
                self.error = @"1";
            }if ([error  isEqual: @"10003"]) {
                self.error = @"1";
            }if ([error  isEqual: @"10004"]) {
                self.error = @"1";
            }if ([error  isEqual: @"10005"]) {
                self.error = @"1";
            }if ([error  isEqual: @"10007"]) {
                self.error = @"1";
            }if ([error  isEqual: @"10008"]) {
                self.error = @"1";
            }if ([error  isEqual: @"10009"]) {
                self.error = @"1";
            }if ([error  isEqual: @"100011"]) {
                self.error = @"1";
            }if ([error  isEqual: @"100012"]) {
                self.error = @"1";
            }if ([error  isEqual: @"100013"]) {
                self.error = @"1";
            }if ([error  isEqual: @"100014"]) {
                self.error = @"1";
            }if ([error  isEqual: @"100020"]) {
                self.error = @"1";
            }if ([error  isEqual: @"100021"]) {
                self.error = @"1";
            }
            
        }
        
        if (block) {
            block();
        }
        
    }];

}
@end
